# -*- coding: utf-8 -*-
# @Time    : 2024/12/9  15:49
# @Author  : Nabby
# @FileName: jasco_declaration_information_impl.py
# @Desc    : 申报信息
from app import db
from infrastructure.po.mode.jasco_cost_details import (
    JascoCostDetails,
)
from sqlalchemy import and_, or_, desc, asc


class JascoCostDetailsImpl(object):

    @classmethod
    def get_cost_details_list(cls, order_id, page=1, page_size=999):
        query = db.session.query(JascoCostDetails).filter(
            JascoCostDetails.order_id == order_id
        )
        return (
            query.order_by(JascoCostDetails.create_time.desc()).paginate(
                page=page, per_page=page_size
            ),
            query.count(),
        )

    @classmethod
    def add_cost_details(cls, order_id, item, cost, currency, ssr, settlement_status):

        jasco_cost_details = JascoCostDetails(
            order_id=order_id,
            item=item,
            cost=cost,
            currency=currency,
            ssr=ssr,
            settlement_status=settlement_status,
        )
        db.session.add(jasco_cost_details)
        db.session.flush()
        db.session.commit()
        return True

    @classmethod
    def edit_cost_details(
        cls, id, order_id, item, cost, currency, ssr, settlement_status
    ):
        jasco_cost_details = (
            db.session.query(JascoCostDetails)
            .filter(
                and_(JascoCostDetails.id == id, JascoCostDetails.order_id == order_id)
            )
            .first()
        )
        if not jasco_cost_details:
            return False
        jasco_cost_details.item = item
        jasco_cost_details.cost = cost
        jasco_cost_details.currency = currency
        jasco_cost_details.ssr = ssr
        jasco_cost_details.settlement_status = settlement_status
        db.session.commit()
        return True

    @classmethod
    def delete_cost_details(cls, id, order_id):
        jasco_cost_details = (
            db.session.query(JascoCostDetails)
           .filter(
                and_(JascoCostDetails.id == id, JascoCostDetails.order_id == order_id)
            )
           .first()
        )
        if not jasco_cost_details:
            return False
        db.session.delete(jasco_cost_details)
        db.session.commit()
        return True
